//
//  CityTableViewCell.m
//  NBTripe
//
//  Created by Xy on 16/1/7.
//  Copyright © 2016年 Vicki. All rights reserved.
//

#import "CityTableViewCell.h"

@interface CityTableViewCell ()
@property (nonatomic,strong) UIImageView *backGround;
@end

@implementation CityTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 8;
        self.imageViews = [[UIImageView alloc]initWithFrame:CGRectZero];
        
        self.userImage = [[UIImageView alloc]initWithFrame:CGRectZero];
        _userImage.layer.borderWidth = 4;
        _userImage.layer.borderColor = [UIColor whiteColor].CGColor;
        _userImage.layer.masksToBounds = YES;
        _userImage.layer.cornerRadius = 40;
        
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _titleLabel.font = [UIFont boldSystemFontOfSize:20];
        _titleLabel.numberOfLines = 0;
        
        
        self.dateLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _dateLabel.textColor = [UIColor grayColor];
        _dateLabel.font = [UIFont systemFontOfSize:13];
        
        self.dressLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _dressLabel.textColor = [UIColor grayColor];
        _dressLabel.font = [UIFont systemFontOfSize:13];
        self.loveLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _loveLabel.textColor = [UIColor darkGrayColor];
        _loveLabel.font = [UIFont systemFontOfSize:13];
        self.priceLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _priceLabel.font = [UIFont boldSystemFontOfSize:23];
        _priceLabel.textColor = [UIColor redColor];
        
        self.tab1 = [[UILabel alloc]initWithFrame:CGRectZero];
        [self labelSign:self.tab1];
        
        
        self.tab2 = [[UILabel alloc]initWithFrame:CGRectZero];
        [self labelSign:self.tab2];
        
        
        self.tab3 = [[UILabel alloc]initWithFrame:CGRectZero];
        [self labelSign:self.tab3];
        
        
        self.is_new = [[UILabel alloc]initWithFrame:CGRectZero];
        _is_new.font = [UIFont boldSystemFontOfSize:20];
        _is_new.textColor = [UIColor redColor];
        
        
        [self addSubview:self.imageViews];
        
        [self addSubview:self.titleLabel];
        [self addSubview:self.dateLabel];
        [self addSubview:self.dressLabel];
        [self addSubview:self.loveLabel];
        [self addSubview:self.priceLabel];
        [self addSubview:self.tab1];
        [self addSubview:self.tab2];
        [self addSubview:self.tab3];
        [self addSubview:self.userImage];
        [self addSubview:self.is_new];
        
    }
    return self;
}


-(void)setCityModel:(CityModel *)cityModel {

    if (_cityModel != cityModel) {
        _cityModel = cityModel;
    }
        
        [self.imageViews sd_setImageWithURL:[NSURL URLWithString:cityModel.title_page]placeholderImage:[UIImage imageNamed:@"160"]];
        [self.userImage sd_setImageWithURL:[NSURL URLWithString:cityModel.users.avatar_l]];
        
        self.titleLabel.text = cityModel.title;
        
        NSString *str = [NSString stringWithFormat:@"%@", cityModel.date_str];
        self.dateLabel.text = str;
        self.dressLabel.text = cityModel.address;
        NSString *str2 = [NSString stringWithFormat:@"%@人喜欢", cityModel.like_count];
        self.loveLabel.text = str2;
        NSString *str3 = [NSString stringWithFormat:@"¥%@", cityModel.price];
        self.priceLabel.text = str3;
        self.is_new.text = cityModel.is_new;
        
        NSArray *array = [NSArray arrayWithArray:cityModel.tab_list];
        NSInteger integer = array.count;
        
        
    if (integer)  {
        if (integer == 1 ) {
            
            
            self.tab1.text = array[0];
            [self labelMuch:self.tab1];
            
            
        }
        else if (integer == 2) {
            
            self.tab1.text = array[0];
            [self labelMuch:self.tab1];
            
            self.tab2.text = array[1];
            [self labelMuch:self.tab2];
            
            
        }
        else  {
            
            self.tab1.text = array[0];
            [self labelMuch:self.tab1];
            
            self.tab2.text = array[1];
            [self labelMuch:self.tab2];
            
            self.tab3.text = array[2];
            [self labelMuch:self.tab3];
            
            
        }
    }
    
        
        
        
    
}


-(void)layoutSubviews {
    [super layoutSubviews];
    
    self.imageViews.frame = CGRectMake(0, 0, self.width, (self.height-10) * 0.6);
    self.userImage.frame = CGRectMake(self.imageViews.right - 100, self.imageViews.bottom - 40, 80, 80);
    
    //    定义一个字典存储label上的字体等信息
    NSDictionary *dic = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20]};
    //    通过字典计算高度和宽度
    CGRect rect = [self.titleLabel.text boundingRectWithSize:CGSizeMake(self.width*0.7, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //    保持原本坐标不变， 只更改高度
    self.titleLabel.frame = CGRectMake(10, self.imageViews.bottom + 10, self.width*0.7, rect.size.height);
    
    CGFloat a = [self getWidthWithText:self.dateLabel.text withFont:20];
    
    self.dateLabel.frame = CGRectMake(10, self.titleLabel.bottom, a, 30);
    
    CGFloat b = [self getWidthWithText:self.dressLabel.text withFont:13];
    if (b > self.width *0.38) {
        b = self.width *0.38;
    }
    self.dressLabel.frame = CGRectMake(self.dateLabel.right, self.titleLabel.bottom, b, 30);
    self.loveLabel.frame = CGRectMake(self.width - 70, self.imageViews.bottom + 60, 70, 30);
    self.tab1.frame = CGRectMake(10, self.height - 40, self.width * 0.21, 30);
    self.tab2.frame = CGRectMake(self.tab1.right + 5, self.height - 40, self.width * 0.21, 30);
    self.tab3.frame = CGRectMake(self.tab2.right + 5, self.height - 40, self.width * 0.21, 30);
    self.priceLabel.frame = CGRectMake(self.width - 80, self.height - 50, 75, 50);
    self.is_new.frame = CGRectMake(self.width - 60, 30, 60, 30);
    
    
}

- (void)labelMuch:(UILabel *)sender {
    sender.layer.borderWidth = 2;
    sender.layer.borderColor = [UIColor grayColor].CGColor;
    sender.layer.masksToBounds = YES;
    sender.layer.cornerRadius = 4;
}


//自适应label
- (CGFloat)getWidthWithText:(NSString *)text withFont:(CGFloat)font {
    NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:font]};
    CGRect rect = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.width;
}

- (CGFloat)getHeightWithText:(NSString *)text withFont:(CGFloat)font {
    NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:font]};
    CGRect rect = [text boundingRectWithSize:CGSizeMake(0, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}


- (void)createView {
    [self removeAllSubviews];
    self.imageViews = [[UIImageView alloc]initWithFrame:CGRectZero];
    
    self.userImage = [[UIImageView alloc]initWithFrame:CGRectZero];
    _userImage.layer.borderWidth = 4;
    _userImage.layer.borderColor = [UIColor whiteColor].CGColor;
    _userImage.layer.masksToBounds = YES;
    _userImage.layer.cornerRadius = 40;
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    _titleLabel.font = [UIFont boldSystemFontOfSize:20];
    _titleLabel.numberOfLines = 0;
    
    
    self.dateLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    _dateLabel.textColor = [UIColor grayColor];
    _dateLabel.font = [UIFont systemFontOfSize:13];
    
    self.dressLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    _dressLabel.textColor = [UIColor grayColor];
    _dressLabel.font = [UIFont systemFontOfSize:13];
    self.loveLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    _loveLabel.textColor = [UIColor darkGrayColor];
    _loveLabel.font = [UIFont systemFontOfSize:15];
    self.priceLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    _priceLabel.font = [UIFont boldSystemFontOfSize:23];
    _priceLabel.textColor = [UIColor redColor];
    
    self.tab1 = [[UILabel alloc]initWithFrame:CGRectZero];
    [self labelSign:self.tab1];
    
    
    self.tab2 = [[UILabel alloc]initWithFrame:CGRectZero];
    [self labelSign:self.tab2];
    
    
    self.tab3 = [[UILabel alloc]initWithFrame:CGRectZero];
    [self labelSign:self.tab3];
    
    
    self.is_new = [[UILabel alloc]initWithFrame:CGRectZero];
    _is_new.font = [UIFont boldSystemFontOfSize:20];
    _is_new.textColor = [UIColor redColor];
    
    
    [self addSubview:self.imageViews];
    [self addSubview:self.userImage];
    [self addSubview:self.titleLabel];
    [self addSubview:self.dateLabel];
    [self addSubview:self.dressLabel];
    [self addSubview:self.loveLabel];
    [self addSubview:self.priceLabel];
    [self addSubview:self.tab1];
    [self addSubview:self.tab2];
    [self addSubview:self.tab3];
    [self addSubview:self.is_new];
    
}

- (void)labelSign:(UILabel *)label {
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor grayColor];
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
